{% extends "admin/base.html" %}

{% block title %}链接分类管理{% endblock %}

{% block page_title %}链接分类管理{% endblock %}

{% block content %}
<div class="card">
    <div class="card-header" style="display: flex; justify-content: space-between; align-items: center;">
        <h2>链接分类列表</h2>
        <div>
            <button type="button" class="btn btn-danger" id="batchDeleteBtn" disabled style="margin-right: 10px;">批量删除</button>
            <a href="/admin/link-categories/add" class="btn btn-primary">添加分类</a>
        </div>
    </div>
    <div class="card-body">
        {% if categories %}
        <form id="batchDeleteForm" action="/admin/link-categories/batch-delete" method="post">
        <table class="table">
            <thead>
                <tr>
                    <th>
                        <input type="checkbox" id="selectAll">
                    </th>
                    <th>ID</th>
                    <th>分类名称</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% if error %}
                <div class="alert alert-danger" style="margin-bottom: 20px;">{{ error }}</div>
                {% endif %}
                {% for category in categories %}
                <tr>
                    <td>
                        {% if category.is_builtin %}
                        <input type="checkbox" name="category_ids" value="{{ category.id }}" class="category-checkbox" disabled>
                        {% else %}
                        <input type="checkbox" name="category_ids" value="{{ category.id }}" class="category-checkbox">
                        {% endif %}
                    </td>
                    <td>{{ category.id }}</td>
                    <td>
                        {{ category.name }}
                        {% if category.is_builtin %}
                        <span class="badge" style="background-color: #f39c12; color: white; margin-left: 5px; font-size: 10px;">内置</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if category.status %}
                        <span style="color: #2ecc71;">启用</span>
                        {% else %}
                        <span style="color: #e74c3c;">禁用</span>
                        {% endif %}
                        {% if not category.is_builtin %}
                        <button type="button" class="btn btn-xs {% if category.status %}btn-warning{% else %}btn-success{% endif %} toggle-status-btn" data-id="{{ category.id }}" data-status="{{ category.status }}" style="margin-left: 5px;">
                            {% if category.status %}禁用{% else %}启用{% endif %}
                        </button>
                        {% endif %}
                    </td>
                    <td>
                        {% if category.is_builtin %}
                        <span class="btn btn-secondary" style="margin-right: 10px; opacity: 0.5; cursor: not-allowed;">编辑</span>
                        <span class="btn btn-danger" style="opacity: 0.5; cursor: not-allowed;">删除</span>
                        {% else %}
                        <a href="/admin/link-categories/edit/{{ category.id }}" class="btn btn-secondary" style="margin-right: 10px;">编辑</a>
                        <button type="button" class="btn btn-danger delete-btn" data-id="{{ category.id }}">删除</button>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
        </form>

<!-- 分页控件 -->
<div class="pagination-container mt-3 text-right">
    <div class="btn-group d-inline-block">
        {% if current_page == 1 %}
        <button class="btn btn-default btn-sm disabled" disabled>首页</button>
        <button class="btn btn-default btn-sm disabled" disabled>上一页</button>
        {% else %}
        <a href="/admin/link-categories?page={{ 1 }}" 
           class="btn btn-default btn-sm">
            首页
        </a>
        <a href="/admin/link-categories?page={{ current_page - 1 }}" 
           class="btn btn-default btn-sm">
            上一页
        </a>
        {% endif %}
        {% for i in range(1, total_pages + 1) %}
            {% if i == current_page %}
                <a href="/admin/link-categories?page={{ i }}" 
                   class="btn btn-primary btn-sm">{{ i }}</a>
            {% else %}
                <a href="/admin/link-categories?page={{ i }}" 
                   class="btn btn-default btn-sm">{{ i }}</a>
            {% endif %}
        {% endfor %}
        {% if current_page == total_pages %}
        <button class="btn btn-default btn-sm disabled" disabled>下一页</button>
        <button class="btn btn-default btn-sm disabled" disabled>末页</button>
        {% else %}
        <a href="/admin/link-categories?page={{ current_page + 1 }}" 
           class="btn btn-default btn-sm">
            下一页
        </a>
        <a href="/admin/link-categories?page={{ total_pages }}" 
           class="btn btn-default btn-sm">
            末页
        </a>
        {% endif %}
    </div>
</div>
</div>
</div>

        <script src="/static/js/admin/link-categories.js"></script>
        {% else %}
        <div style="text-align: center; padding: 40px; color: #666;">
            <p>暂无分类，请点击上方"添加分类"按钮添加</p>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}